package com.example.cardgame

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.graphics.BitmapFactory
import android.net.Uri
import android.os.Bundle
import android.widget.Button
import android.widget.CheckBox
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.RadioButton
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.FileProvider
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.google.android.material.textfield.TextInputEditText
import java.io.File

class MessageActivity : AppCompatActivity() {
    private lateinit var resultLauncher: ActivityResultLauncher<Uri>
    private lateinit var photoUri: Uri
    private  lateinit var imageView: ImageView
    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_message)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        imageView = findViewById(R.id.imageView)

        resultLauncher = registerForActivityResult(ActivityResultContracts.TakePicture()) { success ->
            if (success) {
                val inputStream = contentResolver.openInputStream(photoUri)
                val bitmap = BitmapFactory.decodeStream(inputStream)
                imageView.setImageBitmap(bitmap)
            }
        }
        val modifiedButton = findViewById<ImageButton>(R.id.imageButton2)
        modifiedButton.setOnClickListener {
            var photoFile = File.createTempFile("photo", ".jpg", externalCacheDir)
            photoUri = FileProvider.getUriForFile(this, "com.example.cardgame.fileprovider", photoFile)
            resultLauncher.launch(photoUri)
        }

        imageView.setOnClickListener {
            val intent = Intent(Intent.ACTION_PICK)
            intent.type = "image/*"
            startActivityForResult(intent, REQUEST_CODE_PICK_IMAGE)
        }

        val username = findViewById<TextInputEditText>(R.id.username)
        val stuNum = findViewById<TextInputEditText>(R.id.stuNum)
        val stuPne = findViewById<TextInputEditText>(R.id.stuPne)
        val stuEml = findViewById<TextInputEditText>(R.id.stuEml)
        val hobby1 = findViewById<CheckBox>(R.id.checkBox)
        val hobby2 = findViewById<CheckBox>(R.id.checkBox2)
        val hobby3 = findViewById<CheckBox>(R.id.checkBox3)
        val hobby4 = findViewById<CheckBox>(R.id.checkBox4)
        val sexFemale = findViewById<RadioButton>(R.id.radioButton)

        val button_submit = findViewById<Button>(R.id.submit)
        button_submit.setOnClickListener {
            //there go tomorrows work
            val resultIntent = Intent()
            resultIntent.putExtra("username", username.text.toString())
            resultIntent.putExtra("stuNum", stuNum.text.toString())
            resultIntent.putExtra("stuPne", stuPne.text.toString())
            resultIntent.putExtra("stuEml", stuEml.text.toString())
            resultIntent.putExtra("hobby1", hobby1.isChecked)
            resultIntent.putExtra("hobby2", hobby2.isChecked)
            resultIntent.putExtra("hobby3", hobby3.isChecked)
            resultIntent.putExtra("hobby4", hobby4.isChecked)
            resultIntent.putExtra("sex",if (sexFemale.isChecked) "女" else "男")
            resultIntent.putExtra("photoUri", photoUri.toString())
            setResult(RESULT_OK, resultIntent)
            finish()
        }
        val button_cancel = findViewById<Button>(R.id.cancel)
        button_cancel.setOnClickListener {
            setResult(Activity.RESULT_CANCELED)
            finish()
        }
    }
    companion object {
        private const val REQUEST_CODE_PICK_IMAGE = 1
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_CODE_PICK_IMAGE && resultCode == Activity.RESULT_OK) {
            data?.data?.let { uri ->
                photoUri = uri
                val inputStream = contentResolver.openInputStream(uri)
                val bitmap = BitmapFactory.decodeStream(inputStream)
                imageView.setImageBitmap(bitmap)
            }
        }
    }
}